import 'package:flutter/material.dart';

import 'package:hzy_project_template/commpontents/commpontents_index.dart';
import 'package:hzy_project_template/config/config_index.dart';
import 'package:hzy_project_template/pages/my/widgets/compontents_index.dart';
import 'package:hzy_project_template/pages/my/config/my_normal_config.dart';
import 'package:hzy_project_template/utils/utils_index.dart';

class MyPage extends StatefulWidget {
  const MyPage({super.key});
  @override
  State<StatefulWidget> createState() {
    return MyState();
  }
}

class MyState extends CommonStateWidget<MyPage> {
  /// 整体 滚动控制器
  ScrollController scrollController = ScrollController();

  /// 导航栏 滑动进度
  double navProgress = 0;

  /// 界面配置项
  MyNormalConfig get config => MyNormalConfig();

  @override
  void initState() {
    /// 监听控制器滚动位置
    scrollController.addListener(() {
      double pg = scrollController.offset / 70.h;
      if (pg > 1.0) {
        pg = 1.0;
      } else if (pg < 0) {
        pg = 0;
      }
      setState(() {
        navProgress = pg;
      });
    });
    super.initState();
  }

  @override
  void dispose() {
    scrollController.dispose();
    super.dispose();
  }

  @override
  Widget createBody({
    required BuildContext context,
    BoxConstraints? constraints,
  }) {
    Widget body = configCustomScrollView();
    body = DecoratedBox(
      decoration: BoxDecoration(
        gradient: LinearGradient(
          colors: [
            HzyCommonColor().whitebackgroundColor,
            HzyCommonColor().backgroudgrey,
          ],
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
          stops: const [
            0.5,
            1,
          ],
        ),
      ),
      child: body,
    );
    return body;
  }

  /// 创建滚动视图
  configCustomScrollView() {
    Widget body = CustomScrollView(
      controller: scrollController,
      slivers: [
        SliverPersistentHeader(
          delegate: MyHeaderSliverDelegate(),
          pinned: true,
        ),
        SliverToBoxAdapter(
          child: SizedBox(
            height: (70).h,
          ),
        ),
        SliverToBoxAdapter(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 15.w),
            child: Column(
              children: [
                MyBannerPage(
                  bannerList: config.bannerList,
                ),
                configNormalItemListWidget(
                  list: config.mySecItemList,
                ),
                Gaps.vGap50,
                Gaps.vGap50,
                Gaps.vGap50,
                Gaps.vGap50,
              ],
            ),
          ),
        ),
      ],
    );

    body = SizedBox.expand(
      child: body,
    );
    body = Stack(
      children: [
        configImageBg(),
        configToolWidget(),
        body,
      ],
    );

    return body;
  }

  /// 创建背景图
  configImageBg() {
    /// 获取滚动图片大小
    double userImgW =
        Tween<double>(begin: 60.h + 70.h, end: 0).transform(navProgress);
    Widget body = Image.asset(
      HzyThemeImageConfig().configImagePath(
        pathkey: HzyImageId.navBgImg,
      ),
      fit: BoxFit.fill,
      height: SizeMacro.navh + userImgW,
      width: SizeMacro.screenWidth,
    );

    return body;
  }

  /// 创建工具栏
  configToolWidget() {
    double userImgW = Tween<double>(begin: 70.h, end: 20.h).transform(
      navProgress,
    );
    Widget body = MyTopToollessV();
    body = Positioned(
      left: 15.w,
      right: 15.w,
      top: SizeMacro.navh + userImgW,
      child: body,
    );
    return body;
  }

  /// 创建通用入口item

  configNormalItemListWidget({
    required List<HzyNormalItemModel> list,
  }) {
    Widget body = Column(
      children: [
        ...List.generate(
          list.length,
          (index) {
            HzyNormalItemModel itemModel = list[index];
            itemModel.rightType = 1;
            if (itemModel.isHintWidget != true) {
              itemModel.height = 60.h;
            }
            Widget body = configNormalDarkItemWidget(
              itemModel: itemModel,
              index: index,
              allNum: list.length,
            );
            return body;
          },
        )
      ],
    );

    body = DecoratedBox(
      decoration: BoxDecoration(
        boxShadow: [
          configShadow(),
        ],
      ),
      child: body,
    );
    return body;
  }

  ///
  configTapItem({
    required HzyNormalItemModel model,
  }) {
    if (model.tapType == 1 && model.router != null) {}
  }
}
